<?php
	namespace app\admin\controller;
	use app\common\model\LibGroup;
	use app\common\model\LibMenu;
	use app\common\model\LibAdmin;
	use app\common\controller\AdminController;
	use common\Common;

	class Group extends AdminController{
		protected $group;

		public function __construct(){
			$this->rightVerify(session('admin'), url("admin/Login/login"));
			parent::__construct();
			$this->group=new LibGroup();
		}

		/**
		 * 权限分组角色列表
		 * @return [type] [description]
		 */
		public function groupList(){
			$this->getSetMenu($this);
			$this->log(__CLASS__,__FUNCTION__,"权限分组角色页面",1,"view");
			return $this->fetch("Group/groupList/groupList");
		}

		/**
		 * 添加
		 */
		public function addGroup(){
			$this->getMenu($this);

			$obj=new LibMenu();
			$menu = $obj->findAllMenuAction();
			//dump($menu);
			$this->log(__CLASS__,__FUNCTION__,"添加权限分组角色页面",1,"view");
			return $this->fetch("Group/addGroup/addGroup",['menu'=>$menu]);
		}

		/**
		 * 处理添加信息
		 */
		public function insertGroup(){
			$groupInfo=input("post.");
			$result=$this->group->addGroup($groupInfo);
			echo json_encode($result);
		}

		/**
		 * 编辑权限分组角色页面
		 */
		public function editGroup(){
			$this->getMenu($this);
			$id=input("id");
			$obj=new LibMenu();
			$menu = $obj->findAllMenuAction();
			$groupInfo=$this->group->findGroup("id={$id}");
			$groupInfo['data']['menu_top_id']=explode(',',$groupInfo['data']['menu_top_id']);
			$groupInfo['data']['menu_id']=explode(',',$groupInfo['data']['menu_id']);
			$groupInfo['data']['action_id']=explode(',',$groupInfo['data']['action_id']);
			$this->log(__CLASS__,__FUNCTION__,"编辑权限分组角色页面",1,"view");
			return $this->fetch("Group/editGroup/editGroup",['group'=>$groupInfo['data'],'menu'=>$menu]);
		}

		/**
		 * 处理编辑信息
		 */
		public function updateGroup(){
			$id=input("id");
			$groupInfo=input("post.");
			$result=$this->group->updateGroup("id={$id}",$groupInfo);
			echo json_encode($result);
		}

		/**
		 * 权限分组角色删除
		 */
		public function deleteGroup(){
			$aid=input("aid");
			//判断当前角色下是否有账户存在
			$obj=new LibAdmin();
			$data=$obj->findAdmin("group_id={$aid}");
			if($data['errorCode']==0){
				$result=Common::errorArray(1,"该角色下有账户存在,不可删除",false);
			}
			else{
				$result=$this->group->deleteGroup("id={$aid}");
			}
			$this->log(__CLASS__, __FUNCTION__, "角色删除", 2, 'view');
			echo json_encode($result);
		}

		/**
		 * 分页显示信息
		 * @return [type] [description]
		 */
		public function pagingGroup(){
			$page = $this->getPageInfo($this);
			$keyValueList = array('title'=>'like');
	   	 	$conditionList = $this->getPagingList($this, $keyValueList);
	   	 	$conditionList[]=array("field"=>"is_show","operator"=>"=","value"=>1);
	   	 	$result=$this->group->pagingGroup($page,$conditionList);
	   	 	echo json_encode($result);
		}
	}
?>